package android.gpsmessenger;

import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class TripListFragment extends ListFragment 
{
	private TripManager tripManager;
    private TripList trips;
    private int itemSelected = -1; 

    public void onResume() 
    {
		super.onResume();
		setListAdapter(getCurrentTrips());
    }

    public void onPause() 
    {
    		super.onPause();
    		tripManager.save(trips);
    }
    
	public void onCreate (Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
		tripManager = new TripManager(getActivity());
		trips = new TripList();
	}
	
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.fragment_list_menu, menu);
	}
	
	public void onPrepareOptionsMenu(Menu menu) 
	{
		super.onPrepareOptionsMenu(menu);
	    MenuItem delete = menu.findItem(R.id.menu_delete);
	    MenuItem edit = menu.findItem(R.id.menu_edit);
	    MenuItem select = menu.findItem(R.id.menu_select);
		if (itemSelected != -1) {
			delete.setEnabled(true);
			edit.setEnabled(true);
			select.setEnabled(true);
		} else {
			delete.setEnabled(false);
			edit.setEnabled(false);	
			select.setEnabled(false);
		}
	}
	
	public boolean onOptionsItemSelected (MenuItem item) 
	{
        Intent intent = new Intent(getActivity(), TripProfileActivity.class);
        Intent intent2 = new Intent(getActivity(), CurrentTripActivity.class);
		if (item.getItemId() == R.id.menu_new) 
		{
	        getActivity().startActivity(intent);			
		}
		if (item.getItemId() == R.id.menu_edit) 
		{
			TextView text = (TextView) getListView().getChildAt(itemSelected);
			intent.putExtra("name", text.getText().toString());
	        getActivity().startActivity(intent);
	        itemSelected = -1;
    		getActivity().invalidateOptionsMenu();
		}
		if (item.getItemId() == R.id.menu_delete) 
		{
			TextView text = (TextView) getListView().getChildAt(itemSelected);
			remove_name(text.getText().toString());
		}
		if (item.getItemId() == R.id.menu_select) 
		{
			TextView text2 = (TextView) getListView().getChildAt(itemSelected);
			intent2.putExtra("selected_name", text2.getText().toString());
			getActivity().startActivity(intent2);	
	        itemSelected = -1;
    		getActivity().invalidateOptionsMenu();
		}
		if (item.getItemId() == R.id.menu_home) 
		{
			getActivity().finish();
		}				
		return true;
	}
	
    public void onListItemClick(ListView l, View v, int position, long id) 
    {
    		itemSelected = position;
    		getActivity().invalidateOptionsMenu();
    }
	
	public void onActivityCreated(Bundle savedInstanceState) 
	{
		super.onActivityCreated(savedInstanceState);
		setEmptyText("No Profiles.");
		setListAdapter(getCurrentTrips());
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
	}
	
	public void remove_name(String name) 
	{
		trips.removeTrip(name);
		tripManager.save(trips);
		setListAdapter(getCurrentTrips());
		itemSelected = -1;
		getActivity().invalidateOptionsMenu();
	}
	
    public ArrayAdapter<String> getCurrentTrips() 
    {
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_single_choice);

        trips.clear();

        if (tripManager.load() != null)
        {
	        trips = tripManager.load();
	        
	        for (String name : trips) 
	        {
	        	adapter.add(name);
	        }
        }
        
        return adapter;
    }

}
